/**
 * @file Body_Health_Clcok_controller.ino
 * @brief The controller part of project Body Health Clock
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/07
 *
 * This is the main entrance to the program in Arduino.
 *
 */

#include "sensor/walk_step_counter.h"
#include "sensor/health_module.h"
#include "sensor/temperature_module.h"
#include "SD.h"
#include "SPI.h"
#include <time.h>

#define DATA_SERIAL Serial // debug
// #define DATA_SERIAL Serial3 // default
unsigned int lastTime = 0;
bool is_sensor_open = false;

void setup()
{

    Serial.begin(9600);
    Serial1.begin(38400);
    DATA_SERIAL.begin(115200);
}

void loop()
{
    if (DATA_SERIAL.available() && !is_sensor_open)
    {
        String command = DATA_SERIAL.readStringUntil('\n');
        Serial.println(command);
        bool tmp = false;
        String begin_command = "sensor begin";
        for (int i = 0; i < begin_command.length(); i++)
        {
            if (begin_command[i] == command[i])
            {
                if (i == begin_command.length() - 1)
                {
                    tmp = true;
                }
                continue;
            }
            else
            {
                break;
            }
        }
        if (tmp)
        {
            delay(3000);
            Serial.println("sensor open");
            health_module_setup();
            temperature_module_setup();
            walk_step_counter_setup();
            DATA_SERIAL.println("sensor open");
            is_sensor_open = true;
        }
    } else if (DATA_SERIAL.available() && is_sensor_open)
    {
        String command = DATA_SERIAL.readStringUntil('\n');
        bool tmp = false;
        String begin_command = "sensor close";
        for (int i = 0; i < begin_command.length(); i++)
        {
            if (begin_command[i] == command[i])
            {
                if (i == begin_command.length() - 1)
                {
                    tmp = true;
                }
                continue;
            }
            else
            {
                break;
            }
        }
        if (tmp)
        {
            health_module_close();
            is_sensor_open = false;
        }
    }

    if (is_sensor_open)
    {
        walk_step_counter_loop();
        String result_for_step_counter = "2_" + String(walk_step_counter);
        Serial.println(result_for_step_counter);
        DATA_SERIAL.println(result_for_step_counter);


        temperature_module_loop();
        String result_for_temperature = "0_" + String(temp);
        Serial.println(result_for_temperature);
        DATA_SERIAL.println(result_for_temperature);

        health_module_loop();
        if (health_module_has_data)
        {
            uint8_t hr = bhs.heartrate;
            uint8_t bo = bhs.spo2;
            uint8_t bph = bhs.rsv3;
            uint8_t bpl = bhs.rsv4;

            String result_for_health = "1_";
            result_for_health += String(bph) + "_" + String(bpl) + "_" + String(bo) + "_" + String(hr);
            Serial.println(result_for_health);

            DATA_SERIAL.println(result_for_health);
            health_module_has_data = false;
        }
        else
        {
            unsigned int currentTime = millis();
            if (currentTime - lastTime >= 2000)
            {
                lastTime = currentTime;
                Serial.println("Health moudle doesn't has data.");
            }
        }
    }
}